using System;
using System.Collections.Generic;
using LaxCraft.Domain;

namespace LaxCraft.Repositories
{
	using NHibernate;

	using LaxCraft.Plumbing;

	public class UserRepository : IUserRepository
	{
		private readonly ISession session;

		public UserRepository(ISession session)
		{
			this.session = session;
		}

	    public IEnumerable<User> GetUsers()
	    {
            using (var tx = session.BeginTransaction()) {
                var data = session.QueryOver<User>().Future();
                tx.Commit();
                return data;
            }
	    }

	    public bool Contains(string email)
	    {
            using (var tx = session.BeginTransaction()) {
                var data = session.QueryOver<User>().Where(u => u.Email == email).RowCount() > 0;
                tx.Commit();
                return data;
            }
	    }

	    public User GetUser(string email)
	    {
            using (var tx = session.BeginTransaction()) {
                var data = session.QueryOver<User>().Where(u => u.Email == email).Take(1).List()[0];
                tx.Commit();
                return data;
            }
	    }

	    public void SaveUser(User user)
	    {
            using (var txn = session.BeginTransaction()) {
                session.Save(user);
                txn.Commit();
            }
	    }
	}
}